package com.xuecheng.content.service.impl;

import com.alibaba.fastjson.JSON;
import com.xuecheng.base.exception.XuechengPlusException;
import com.xuecheng.content.mapper.CourseBaseMapper;
import com.xuecheng.content.mapper.CoursePublishMapper;
import com.xuecheng.content.mapper.CoursePublishPreMapper;
import com.xuecheng.content.model.dto.CourseBaseInfoDto;
import com.xuecheng.content.model.dto.CoursePreviewDto;
import com.xuecheng.content.model.dto.TeachPlanDto;
import com.xuecheng.content.model.enums.AuditStatusEnum;
import com.xuecheng.content.model.enums.PublishStatus;
import com.xuecheng.content.model.po.CourseBase;
import com.xuecheng.content.model.po.CoursePublish;
import com.xuecheng.content.model.po.CoursePublishPre;
import com.xuecheng.content.service.CourseBaseInfoService;
import com.xuecheng.content.service.CoursePublishService;
import com.xuecheng.content.service.TeachPlanService;
import com.xuecheng.messagesdk.model.po.MqMessage;
import com.xuecheng.messagesdk.service.MqMessageService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class CoursePublishServiceImpl implements CoursePublishService {

    @Autowired
    private CourseBaseInfoService courseBaseInfoService;
    @Autowired
    private TeachPlanService teachPlanService;
    @Autowired
    private CoursePublishPreMapper coursePublishPreMapper;
    @Autowired
    private CourseBaseMapper courseBaseMapper;
    @Autowired
    private CoursePublishMapper coursePublishMapper;
    @Autowired
    private MqMessageService mqMessageService;

    /**
     * @description 获取课程预览信息
     * @param courseId 课程id
     * @return com.xuecheng.content.model.dto.CoursePreviewDto
     * @author Mr.M
     * @date 2022/9/16 15:36
     */
    @Override
    public CoursePreviewDto getCoursePreviewInfo(Long courseId) {
        //查询课程基本信息和计划表信息
        CoursePreviewDto coursePreviewInfo = new CoursePreviewDto();

        //查询课程基本信息
        CourseBaseInfoDto courseBaseInfo = courseBaseInfoService.getCourseBaseInfo(courseId);

        //查询课程计划信息
        List<TeachPlanDto> teachPlanTree = teachPlanService.getTeachPlanTree(courseId);

        coursePreviewInfo.setCourseBase(courseBaseInfo);
        coursePreviewInfo.setTeachplans(teachPlanTree);
        return coursePreviewInfo;
    }

    /**
     * @description 提交审核
     * @param companyId 机构id
     * @param courseId 课程id
     */
    @Override
    @Transactional
    public void commitAudit(Long companyId, Long courseId) {

        CoursePublishPre coursePublishPre = new CoursePublishPre();
        //先查询课程的发布信息
        CourseBaseInfoDto courseBaseInfo = courseBaseInfoService.getCourseBaseInfo(courseId);
        //数据检验，如果课程是已提交状态，那么不允许在提交
        if(AuditStatusEnum.SUBMIT.equals(courseBaseInfo.getAuditStatus())){
            XuechengPlusException.cast("课程已提交，请勿重复提交");
        }
        //本机构的只能提交本机构的课程
        if(!companyId.equals(courseBaseInfo.getCompanyId())){
            XuechengPlusException.cast("不能提交其他机构的课程");
        }

        //课程图片是否填写
        if(courseBaseInfo.getPic() == null){
            XuechengPlusException.cast("请上传课程图片");
        }
        BeanUtils.copyProperties(courseBaseInfo,coursePublishPre);
        //课程计划信息是否填写
        List<TeachPlanDto> teachPlanTree = teachPlanService.getTeachPlanTree(courseId);
        if(teachPlanTree == null || teachPlanTree.size() == 0){
            XuechengPlusException.cast("请填写课程计划");
        }
        //整合课程发布表信息
        //课程计划信息再课程发布表中的类型是字符串所以要转一下
        String teachPlanTreeString = JSON.toJSONString(teachPlanTree);

        coursePublishPre.setStatus("202003");
        coursePublishPre.setCompanyId(companyId);
        coursePublishPre.setTeachplan(teachPlanTreeString);
        coursePublishPre.setCreateDate(LocalDateTime.now());
        coursePublishPre.setId(courseId);

        //如果在课程存在，那么就更新，不存在就插入
        CoursePublishPre coursePublishPre1 = coursePublishPreMapper.selectById(courseId);
        if(coursePublishPre1 != null){
            //更新
            coursePublishPreMapper.updateById(coursePublishPre);
        }else{
            //插入
            coursePublishPreMapper.insert(coursePublishPre);
        }

        //更新课程基本信息表的审核状态
        CourseBase courseBase = new CourseBase();
        BeanUtils.copyProperties(courseBaseInfo,courseBase);
        courseBase.setAuditStatus(AuditStatusEnum.SUBMIT);
        //更新
        courseBaseMapper.updateById(courseBase);
    }

    /**
     * @description 课程发布
     * @param companyId 机构id
     * @param courseId 课程id
     */
    @Override
    public void publish(Long companyId, Long courseId) {
        //从课程预发布表中获取到记录，如果存在则更新
        CoursePublishPre coursePublishPre = coursePublishPreMapper.selectById(courseId);
        if(coursePublishPre == null){
            XuechengPlusException.cast("请先提交课程审核");
        }
        if(!coursePublishPre.getStatus().equals("202004")){
            XuechengPlusException.cast("课程没有审核通过，不能发布");
        }
        //只有同一机构才能发布
        if(!companyId.equals(coursePublishPre.getCompanyId())){
            XuechengPlusException.cast("不能发布其他机构的课程");
        }
        //在课程发布表中获取记录，如果存在则更新
        CoursePublish coursePublish = coursePublishMapper.selectById(courseId);
        if(coursePublish != null){
            //更新
            BeanUtils.copyProperties(coursePublishPre,coursePublish);
            coursePublishMapper.updateById(coursePublish);
        }else{
            //插入
            coursePublish = new CoursePublish();
            BeanUtils.copyProperties(coursePublishPre,coursePublish);
            coursePublishPre.setStatus("203002");
            coursePublishMapper.insert(coursePublish);
        }

        //更新课程信息表的课程发布状态为已发布
        CourseBase courseBase = courseBaseMapper.selectById(courseId);
        courseBase.setStatus(PublishStatus.PUB);
        //更新
        courseBaseMapper.updateById(courseBase);

        //删除对应的课程预发布表
        coursePublishPreMapper.deleteById(courseId);

        //保存消息表记录
        saveCoursePublishMessage(courseId);
    }

    /**
     * @description 保存消息表记录，稍后实现
     * @param courseId  课程id
     * @return void
     * @author Mr.M
     * @date 2022/9/20 16:32
     */
    private void saveCoursePublishMessage(Long courseId){

        MqMessage course_publish = mqMessageService.addMessage("course_publish", courseId.toString(), null, null);
        if(course_publish == null){
            XuechengPlusException.cast("添加消息记录失败");
        }



    }
}
